//
//  LLImgCollectionViewCell.m
//  lrlzapp
//
//  Created by dl on 16/2/29.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLImgCollectionViewCell.h"
#import "LLBottomTitleViewModel.h"

@interface LLImgCollectionViewCell ()
@property(nonatomic, strong) LLUIImageView *imageView;
@property(nonatomic, strong) UILabel *title;
@property(nonatomic, strong) LLBottomTitleViewModel *viewModel;
@property(nonatomic, strong) CAGradientLayer *gradLayer;
@end

@implementation LLImgCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];

        _imageView = [[LLUIImageView alloc] init];
        [self addSubview:_imageView];

        self.gradLayer = [CAGradientLayer new];
        self.gradLayer.startPoint = CGPointMake(0, 1);
        self.gradLayer.endPoint = CGPointMake(0, 0);
        self.gradLayer.locations = @[@0, @1];
        self.gradLayer.colors = @[
                (__bridge id) UIColorFromRGBA(0x000000, 0.6).CGColor,
                (__bridge id) UIColorFromRGBA(0x000000, 0).CGColor
        ];
        [self.layer addSublayer:self.gradLayer];

        self.title = [[UILabel alloc] init];
        self.title.textColor = [UIColor whiteColor];
        self.title.font = SUIT_FONT_320(17);
        [self addSubview:self.title];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _imageView.frame = self.bounds;
    self.gradLayer.frame = CGRectMake(
            0,
            CGRectGetHeight(self.bounds) - SUIT_H_ALL(45),
            CGRectGetWidth(self.bounds),
            SUIT_H_ALL(45)
    );
    self.title.frame = CGRectMake(
            SUIT_W(14),
            CGRectGetHeight(self.bounds) - SUIT_H_ALL(25),
            CGRectGetWidth(self.bounds) - 2 * SUIT_W(14),
            SUIT_H_ALL(21)
    );
}

- (void)bindViewModel:(id)viewModel {
    self.viewModel = viewModel;

    @weakify(self)
    [[RACObserve(self.viewModel, imageUrl)
            takeUntil:self.rac_prepareForReuseSignal]
            subscribeNext:^(NSString *imageUrl) {
                @strongify(self)
                [self.imageView setImageUrlStr:imageUrl size:self.viewModel.imageSize];
            }];
    [[[RACObserve(self.viewModel, showGradient)
            not]
            takeUntil:self.rac_prepareForReuseSignal]
            subscribeNext:^(NSNumber *showGradient) {
                @strongify(self)
                self.gradLayer.hidden = [showGradient boolValue];
            }];
    RAC(self.title, hidden) = [[RACObserve(self.viewModel, showTitle)
            not]
            takeUntil:self.rac_prepareForReuseSignal];
    RAC(self.title, text) = [RACObserve(self.viewModel, title)
            takeUntil:self.rac_prepareForReuseSignal];
}

@end
